TLVIVI = {};

TLVIVI.Pacman = {};

TLVIVI.Pacman.GameManager = function(showMessageCallback){
	this._currGameId = -1;
	this._currGameName = "";
	this._apples = null;
	this._gameBorders = null;
	this._currPlayerId = -1;
	this._currPlayerType = -1;
	this._currGamePlayers = null;
	this._showMessageCallback = showMessageCallback;
}

TLVIVI.Pacman.SERVER_URL = "http://tlvivi.co.il/";

TLVIVI.Pacman.GameManager.prototype.getCurrGameId = function(){
	return this._currGameId;
}

TLVIVI.Pacman.GameManager.prototype.getCurrGameName = function(){
	return this._currGameName;
}

TLVIVI.Pacman.GameManager.prototype.getCurrGamePlayers = function(){
	return this._currGamePlayers;
}

TLVIVI.Pacman.GameManager.prototype.getGameBorders = function(){
	return this._gameBorders;
}

TLVIVI.Pacman.GameManager.prototype.getApples = function(){
	return this._apples;
}

TLVIVI.Pacman.GameManager.prototype._setApples = function(apples){
	this._apples = new Hashtable();
	
	for (var index = 0; index < apples.length; index++) {
		this._apples.put(apples[index].EntityId, apples[index]);
	}
}

TLVIVI.Pacman.GameManager.prototype._updateRemovedApples = function(removedApples){
	var self = this;
	$.each(removedApples, function(index, value){
		self._apples.remove(value);
	})
}


// Get the list of available games
TLVIVI.Pacman.GameManager.prototype.getAllGames = function(callBack){
	var context = {callback: callBack};
	
	$.getJSON(TLVIVI.Pacman.SERVER_URL + "GetAllGames.ashx?jsoncallback=?", $.proxy(this._onGetAllGamesCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onGetAllGamesCallback = function(result){
	this.callback(result);
}

	
	
// Create a new game
TLVIVI.Pacman.GameManager.prototype.createNewGame = function(gameName, callBack) {
	var context = {callback: callBack, self: this};
	$.getJSON(TLVIVI.Pacman.SERVER_URL + "CreateGame.ashx?jsoncallback=?&Name=" + gameName, $.proxy(this._onCreateNewGameCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onCreateNewGameCallback = function(result){
	self._currGameId = result.GameId;
	this.callback(result.GameId);
}


// Join an existing game
TLVIVI.Pacman.GameManager.prototype.joinGame = function(gameName, playerName, playerRole, callBack){
	var context = {callback: callBack, self:this, role: playerRole, gameName: gameName};
	
	var url = new StringBuilder(TLVIVI.Pacman.SERVER_URL);
	url.append("JoinGame.ashx?jsoncallback=?");
	url.append("&GameName=");
	url.append(gameName);
	url.append("&PlayerName=");
	url.append(playerName);
	url.append("&Role=");
	url.append(playerRole);
	
	$.getJSON(url.toString(), $.proxy(this._onJoinGameCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onJoinGameCallback = function(result){	
	this.self._currPlayerType = this.role;
	this.self._currPlayerId = result.PlayerId;
	this.self._currGameName = this.gameName;
	this.callback(result);
}



// Get the players of a specified game
TLVIVI.Pacman.GameManager.prototype.getGamePlayers = function(gameName, callBack){
	var context = {callback: callBack, gameName: gameName};
	
	$.getJSON(TLVIVI.Pacman.SERVER_URL + "GetAllGames.ashx?jsoncallback=?", $.proxy(this._onGetGamePlayersCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onGetGamePlayersCallback = function(result){	
	var gameName = this.gameName;
	var callBack = this.callback;
	var isFound = false;
	$.each(result.Games, function(index, value){
		if (value.Name == gameName){
			isFound = true;
			callBack(value.Players);
			return isFound;
		}
	});
	
	if (!isFound){
		callBack(null);
	}
}



// Get the state of a specified game
TLVIVI.Pacman.GameManager.prototype.getGameStatus = function(gameName, callBack){
	var context = {callback: callBack, gameName: gameName};
	
	$.getJSON(TLVIVI.Pacman.SERVER_URL + "GetAllGames.ashx?jsoncallback=?", $.proxy(this._onGetGameStatusCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onGetGameStatusCallback = function(result){
	var gameName = this.gameName;
	var callBack = this.callback;
	var isFound = false;
	
	$.each(result.Games, function(index, value){
		if (value.Name == gameName){
			isFound = true;
			callBack(value.State);
			return isFound;
		}
	});
	
	if (!isFound){
		callBack(null);
	}
}


// Get the current state of the game
TLVIVI.Pacman.GameManager.prototype.getGameState = function(callBack){
	var context = {callback: callBack, self: this};
	
	$.getJSON(TLVIVI.Pacman.SERVER_URL + "GameState.ashx?jsoncallback=?&EntityId=" + this._currPlayerId, $.proxy(this._onGetGameStateCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onGetGameStateCallback = function(result){
	this.self._setApples(result.Apples);
	this.self._gameBorders = result.GameBorderPoints;
	this.callback(result);
	
	if (result.Apples > 15 && this.self._currPlayerType == 2){
		this.self._showMessageCallback("Pacman just ate another 15 apples!");
	}
}



// Update the player state and get an updated state of the game
TLVIVI.Pacman.GameManager.prototype.updateGameState = function(lat, lng, isEnabled, prevSecondsToUpdate, callBack){
	var context = {callback: callBack, self: this};
	
	var url = new StringBuilder();
	url.append(TLVIVI.Pacman.SERVER_URL);
	url.append("PollServer.ashx?jsoncallback=?&EntityId=");
	url.append(this._currPlayerId);
	url.append("&GeoLat=");
	url.append(lat);
	url.append("&GeoLng=");
	url.append(lng);
	url.append("&IsEnabled=");
	url.append(isEnabled);
	url.append("&UpdateMeForTheLastXSeconds=");
	url.append(prevSecondsToUpdate);
	
	
	$.getJSON(url.toString(), $.proxy(this._onUpdateGameStateCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onUpdateGameStateCallback = function(result){
	this.self._currGamePlayers = result.Players;
	this.self._updateRemovedApples(result.RemovedAppleIds);
	
	this.callback(result);
}


TLVIVI.Pacman.GameManager.prototype.sendMessage = function(message, callBack){
	var context = {callback: callBack};
	
	var url = new StringBuilder(TLVIVI.Pacman.SERVER_URL);
	url.append("SendGameMessage.ashx?jsoncallback=?&SenderId=");
	url.append(this._currPlayerId);
	url.append("&MessageText=");
	url.append(message);
	
	$.getJSON(url.toString(), $.proxy(this._onSendMessageCallback, context));
}

TLVIVI.Pacman.GameManager.prototype._onSendMessageCallback = function(result){
	
}
